<template>
	<div>
		<el-select class="w-ipt" v-model="c_project_id" placeholder="请选择项目"
			@change="projectChange" filterable>
			<el-option v-for="(item,index) in project_list" :key="index" :label="item.name"
				:value="item.id">
			</el-option>
			<div v-if="loading_more" class="more">
				<el-button type="primary" size="mini" @click="limitChange">加载更多</el-button>
			</div>
			<div v-else class="nomore">
				<el-button type="text" size="mini" disabled>暂无更多数据</el-button>
			</div>
		</el-select>
	</div>
</template>

<script>
	import API from '@/api/index.js'
	
	export default {
		data() {
			return {
				c_project_id: this.project_id,
				project_list: [],
				loading_more: true,
				page: 1,
				limit: '10'
			}
		},
		props: ['project_id'],
		watch: {
			project_id(val) {
				this.c_project_id = val;
			}
		},
		mounted() {
			this.getProject();
		},
		methods: {
			// 获取百度接口
			getProject() {
				let self = this;
				API.projects(1, 10).then(res => {
					let data = res.data;
					self.$notify({
						message: '获取项目成功'
					});
					self.project_list = data.data;
					self.page = self.page + 1;
				}).catch(err => {
					self.$message.error(err.response.data.message)
				})
			},
			// 加载更多数据
			limitChange() {
				let self = this;
				API.projects(self.page, self.limit).then(res => {
					let data = res.data;
					self.$notify({
						message: '加载项目成功'
					});
					if (data.data.length > 0) {
						self.project_list = self.project_list.concat(data.data);
						self.page = self.page + 1;
					} else {
						self.loading_more = false;
					}
				}).catch(err => {
					self.$message.error(err.response.data.message)
				});
			},
				
			// 选择了项目id
			projectChange(val) {
				let self = this;
				self.$emit('getProjectId', {
					child_project_id: val
				});
			},
		}
	}
</script>

<style>
</style>
